﻿$(function () {
    procuredFan()
})
//采购反填
function procuredFan() {
    var procuredId = localStorage["PASprocuredId"]
    $.ajax({
        url: "/RawMaterial/ProcuredMondel",
        type: "get",
        data: {
            Id: procuredId,
        },
        success: function (data) {
            data = JSON.parse(data);
            console.log(data.data);
            // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
            $(data.data).each(function () {
                $("#ProcuredNumber").text(this.procuredNumber)
                $("#FeedstockName").text(this.feedstockName)
                $("#FeedstockNumber").text(this.feedstockNumber)
                $("#FeedstockNum").text(this.feedstockNum)
                var now = new Date(this.updateTime);
                var year = now.getFullYear()
                var month = ("0" + now.getMonth() + 2).slice(-2);
                var day = ("0" + now.getDate()).slice(-2);
                $("#UpdateTime").text(year + "-" + month + "-" + day)
                $("#LotNumber").text(this.lotNumber)
                storagejs(this.procuredNumber)
            })
        }
    });

}
//入库编号
var storageId = 0;
function storagejs(procuredNumber) {

    $.ajax({
        url: "/Supplier/LedgerInfoStoragesPurchaseNumberShow",
        type: "get",
        data: {
            ProcuredNumber: procuredNumber,
        },
        success: function (data) {
            data = JSON.parse(data);
            console.log(data.data[0]);
            if (data.data[0].status) {
                $("#storage").append(
                    '<div style="width:1000px; height:200px;border-color:black;border-style:solid;border-width:2px;">' +
                    '    <h2>仓库信息</h2>' +
                    '    <div id="storagetr">' +
                    '        <table id="storagetable" >' +
                    '            <tr>' +
                    '                <td>仓库编号：<span id="warehouseNumber"></span></td>' +
                    '                <td>物资编码：<span id="positionNumber"></span></td>' +
                    '            </tr>' +
                    '            <tr>' +
                    '                <td>现存库存数量：<span id="detailsNum"></span></td>' +
                    '                <td>入库时间：<span id="storageDate"></span></td>' +
                    '            </tr>' +
                    '            <tr>' +
                    '                <td>库区名称：<span id="reservoirName"></span></td>' +
                    '                <td>库位名称：<span id="locationName"></span></td>' +
                    '            </tr>' +
                    '        </table>' +
                    '    </div>' +
                    '</div>'
                )
                //仓库信息
                $.ajax({
                    url: "/ProcurementAndStorage/LedgerInfoWarehouseNameList",
                    type: "get",
                    data: {
                        warehouseName: data.data[0].warehouseName,
                    },
                    success: function (t) {
                        t = JSON.parse(t);
                        console.log(t.data);
                        $("#warehouseNumber").text(t.data.warehouseNumber)
                        $("#positionNumber").text(t.data.positionNumber)
                        $("#reservoirName").text(data.data[0].reservoirName)
                        $("#locationName").text(data.data[0].locationName)
                        $("#detailsNum").text(data.data[0].detailsNum)
                        var now = new Date(data.data[0].storageDate);
                        var year = now.getFullYear()
                        var month = ("0" + now.getMonth() + 2).slice(-2);
                        var day = ("0" + now.getDate()).slice(-2);
                        $("#storageDate").text(year + "-" + month + "-" + day)
                    }
                })
            }
            if (data.data[0].status == false) {

                $("#Rku").append('<button onclick="Storage(' + data.data[0].storageId + ')">入库</button>')
            }

            $(document).ready(function () {
                $("#storagetable").css("text-align", "center");
                $("#storagetable").css("border-spacing", "0 20px");
                $("#storagetable").css("border-collapse", "separate");
            });

        }
    });
}

//入库状态修改
function Storage(storageId) {
    $.ajax({
        type: "Post",
        url: "/Storages/StoragesUpdateStatus",  // 替换为实际的后台删除数据接口地址
        data: { id: storageId, token: localStorage.getItem("token") },
        success: function (res) {
            var res = JSON.parse(res)
            if (res.data == 1) {
                //更新原料库存数量
                UpdateInventory(storageId)
            } else {
                layer.alert("修改失败");

            }
        },
        error: function () {
            layer.msg('修改失败，请稍后重试', {
                time: 2000,
                icon: 2
            });
        }
    });
}
//更新原料库存数量
function UpdateInventory(storageId) {
    $.ajax({
        type: "Post",
        url: "/Storages/StoragesUpdateStatus",  // 替换为实际的后台删除数据接口地址
        data: { id: storageId, token: localStorage.getItem("token") },
        success: function (res) {
            var res = JSON.parse(res)
            if (res.data == 1) {
                layer.alert("修改成功");
                location.href = '/ProcurementAndStorage/PASparticulars' // 重新加载数据，刷新表格显示
            } else {
                layer.alert("修改失败");

            }
        },
        error: function () {
            layer.msg('修改失败，请稍后重试', {
                time: 2000,
                icon: 2
            });
        }
    });
}


